﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

namespace Foundation.Markup
{
    public class Store : MarkupExtension
    {
        [TypeConverter(typeof (XamlTypeConverter))]
        public Type ItemType { get; set; }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var itemType = ItemType;
            var methodInfo =
                typeof (Foundation.Store).GetMethod("OfType").MakeGenericMethod(itemType.DeclaringType ?? itemType);
            var item = methodInfo.Invoke(null, new object[] {new object[0]});
            return item;
        }
    }

    public class XamlTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(Type);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null) return null;
            var typeName = value.ToString().Split(new[] { ':' }).Last();
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var assembly in assemblies)
            {
                var types = assembly.GetTypes();
                var type = types.FirstOrDefault(t => (t.DeclaringType != null && t.DeclaringType.Name == typeName) || t.Name == typeName);
                if (type != null) 
                    return type;
            }

            return null;
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            return value.ToString();
        }
    }
}
